import java.lang.reflect.Array;
import java.util.*;

public class NGuessNumber {

    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("游戏开始");
            List<Integer> answer = generateAnswer();

            for (int i = 0; i < 5; i++) {
                List<Integer> guess = getPlayerInput();
                String msg = compareGuessAnswer(guess, answer);
                if (isWin(msg)) {
                    System.out.println("恭喜你猜对了！！");
                    break;
                } else {
                    System.out.println("重新猜" + msg);
                }
            }

            System.out.println("是否再来一局(yes)");
            if (!"yes".equals(scanner.nextLine())) {
                return;
            }
        }
    }

    public static List<Integer> generateAnswer() {

        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < 4; i++) {
            int newVal = new Random().nextInt(10);
            while (set.contains(newVal)) {
                newVal = new Random().nextInt(10);
            }
            set.add(newVal);
        }
        return new ArrayList<>(set);
    }

    public static List<Integer> getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        List<Integer> rtn = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            int item = scanner.nextInt();
            rtn.add(item);
        }
        return rtn;
    }


    public static String compareGuessAnswer(List<Integer> guessNums, List<Integer> answer) {
        HashSet<Integer> answersSet = new HashSet<>(answer);
        HashSet<Integer> guessNumsSet = new HashSet<>(guessNums);

        int containCount = 0;
        for (Integer item : guessNumsSet) {
            if (answersSet.contains(item)) {
                containCount++;
            }
        }

        int positionCount = 0;
        for (int i = 0; i < 4; i++) {
            if (guessNums.get(i).equals(answer.get(i))) {
                positionCount++;
            }
        }


        return String.format("%sA%sB", positionCount, containCount - positionCount);
    }

    public static boolean isWin(String msg) {
        return msg.startsWith("4");
    }
}
